package org.dromara.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.SysUserPurse;
import org.dromara.system.domain.bo.HomePagePointsBo;
import org.dromara.system.domain.vo.HomePageLotteryInfoVo;
import org.dromara.system.domain.vo.HomePagePointsVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.mapper.SysUserPointsRecordsMapper;
import org.dromara.system.service.ISysUserPurseService;
import org.dromara.system.service.ISysUserService;
import org.dromara.system.service.IndexInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@RequiredArgsConstructor
@Service
public class IndexInfoServiceImpl implements IndexInfoService {

    @Autowired
    private SysUserPointsRecordsMapper sysUserPointsRecordsMapper;
    private final ISysUserPurseService sysUserPurseService;
    private final ISysUserService sysUserService;

    @Override
    public HomePagePointsVo homePagePoints() {
        Long userId = LoginHelper.getUserId();
        HomePagePointsBo allHomePagePointsBo = HomePagePointsBo.builder()
            .userId(userId).build();
        BigDecimal allPoints = sysUserPointsRecordsMapper.queryHomePagePointsBo(allHomePagePointsBo);

        HomePagePointsBo todayHomePagePointsBo = HomePagePointsBo.builder()
            .userId(userId)
            .startTime(DateUtil.beginOfDay(DateUtil.date()))
            .endTime(DateUtil.endOfDay(DateUtil.date()))
            .build();
        BigDecimal todayPoints = sysUserPointsRecordsMapper.queryHomePagePointsBo(todayHomePagePointsBo);
        return HomePagePointsVo.builder()
            .allPoints(allPoints)
            .todayPoints(todayPoints)
            .build();
    }

    @Override
    public HomePageLotteryInfoVo homePageLotteryInfo() {
        Long userId = LoginHelper.getUserId();
        SysUserPurse sysUserPurse = sysUserPurseService.lambdaQuery().eq(SysUserPurse::getUserId, userId).one();
        BigDecimal lotteryPoints = sysUserPurse.getLotteryPoints();
        SysUserVo sysUserVo = sysUserService.selectUserById(userId);
        BigDecimal dealSumTag = sysUserVo.getDealSumTag();
        return HomePageLotteryInfoVo.builder()
            .lotteryPoints(lotteryPoints)
            .DealSumTag(dealSumTag)
            .targetSum(Convert.toBigDecimal(10000))
            .build();
    }
}
